package com.treasure.hunt.main

import android.view.Gravity
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.google.android.material.tabs.TabLayout
import com.module.lemlin.base.BaseAbstractFragment
import com.treasure.hunt.R
import com.treasure.hunt.hunt.HuntGradeFragment
import kotlinx.android.synthetic.main.fragment_main_hunt.*

class MainHuntFragment : BaseAbstractFragment() {

    companion object {
        fun newInstance(): MainHuntFragment {
            return MainHuntFragment()
        }
    }

    private val hunts: MutableList<String> = mutableListOf(
        "普通寻宝", "豪华寻宝"
    )

    override val layoutResId: Int
        get() = R.layout.fragment_main_hunt

    override fun initView() {
        BarUtils.addMarginTopEqualStatusBarHeight(flMainHuntBar)
        tlMainHuntLabel.apply {
            addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabReselected(tab: TabLayout.Tab?) = Unit

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    tab?.customView = null
                }

                override fun onTabSelected(tab: TabLayout.Tab?) {
                    val text: TextView = TextView(context).apply {
                        setTextColor(ColorUtils.getColor(R.color.colorMain))
                        textSize = 16f
                        gravity = Gravity.CENTER
                        paint.isFakeBoldText = true
                    }
                    text.text = tab?.text
                    tab?.customView = text
                }
            })
        }.setupWithViewPager(vpMainHuntBody.apply {
            adapter = object : FragmentStatePagerAdapter(
                childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
            ) {

                override fun getItem(position: Int): Fragment =
                    HuntGradeFragment.newInstance(if (position == 0) 1 else 2)

                override fun getCount(): Int = hunts.size

                override fun getPageTitle(position: Int): CharSequence? {
                    return hunts[position]
                }

                override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {}
            }
        })
    }
}